package org.serverct.finalfight.util;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.serverct.finalfight.FinalFight;

import java.io.File;

public class CustomSettings {
    public static FileConfiguration config;
    public static YamlConfiguration language;

    public static String replace(String origin, StringPair... replacers) {
        for (StringPair replacer : replacers) {
            String key = String.valueOf(replacer.getKey());
            String value = String.valueOf(replacer.getValue());
            origin = origin.replace(key, value);
        }
        return origin;
    }

    public static String getPrefix() {
        return language.getString("prefix", "").replace("&", "§");
    }

    public static String getLanguageWithPrefix(String path) {
        return getPrefix() + language.getString(path, "").replace("&", "§");
    }

    public static String getLanguageWithPrefix(String path, StringPair... replacers) {
        return replace(getLanguageWithPrefix(path), replacers);
    }

    public static String getLanguageWithoutPrefix(String path) {
        return language.getString(path).replace("&", "§");
    }

    public static String getLanguageWithoutPrefix(String path, StringPair... replacers) {
        return replace(getLanguageWithoutPrefix(path), replacers);
    }

    public static File getFile(String directory, String resourceFile) {
        return new File(FinalFight.plugin.getDataFolder() + "/" + directory, resourceFile);
    }

    public static YamlConfiguration getYaml(String directory, String resourceFile) {
        return YamlConfiguration.loadConfiguration(getFile(directory, resourceFile));
    }

    public static void initialize() {
        config = ResourceUtils.autoUpdateConfigs("", "config.yml");
        language = ResourceUtils.autoUpdateConfigs("languages/", config.getString("language"));
    }
}
